package com.whl.sm.controller.parking;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.model.parking.ParkingLots;
import com.whl.sm.service.parking.ParkingLotsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-25
 */
@RestController
@RequestMapping("/parking-lots")
public class ParkingLotsController extends BaseController {

    @Autowired
    private ParkingLotsService parkingLotsService;

    @RequestMapping("/list")
    public Result getParkingLots(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("parking_name");
        setHttpRequestQueryWrapper(queryWrapper,"visible","parking_type");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> lotsPage = parkingLotsService.page(getPage(),queryWrapper);
        return Result.ok(lotsPage);
    }

    @RequestMapping("/info/{id}")
    public Result parkingInfo(@PathVariable("id") String id){
        ParkingLots parkingLots = parkingLotsService.getById(id);
        return Result.ok(parkingLots);
    }


    @RequestMapping("/update")
    public Result updateParking (@RequestBody ParkingLots parkingLots){
        return parkingLotsService.update(parkingLots);
    }


    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            parkingLotsService.update(new UpdateWrapper<ParkingLots>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }


    @RequestMapping("/selectList")
    public Result selectList(){
        List<ParkingLots> lotsList = parkingLotsService.list();
        List<SelectDto> resultList = lotsList.stream().map(obj->{
            return new SelectDto(obj.getId(),obj.getParkingName(),obj.getParkingCode(),false);
        }).collect(Collectors.toList());
        return Result.ok(resultList);
    }




}
